package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageResult;
import com.woniu.mapper.OrganizationMapper;
import com.woniu.mapper.TeamMapper;
import com.woniu.mapper.TeamOrganizationMapper;
import com.woniu.pojo.Organization;
import com.woniu.pojo.Team;
import com.woniu.pojo.TeamOrganization;
import com.woniu.service.IOrganizationService;
import com.woniu.vo.OrganizationQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 机构表 服务实现类
 * </p>
 *
 * @author xiaotang
 * @since 2022-10-10
 */
@Service
@Slf4j
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements IOrganizationService {

    @Autowired
    private OrganizationMapper organizationMapper;
    @Resource
    private TeamOrganizationMapper teamOrganizationMapper;
    @Resource
    private TeamMapper teamMapper;

    /**
     * 搜索+分页
     * @param currentPage
     * @param pageSize
     * @param organizationQueryVo
     * @return
     */
    @Override
    public PageResult search(Integer currentPage, Integer pageSize, OrganizationQueryVo organizationQueryVo) {
        // 创建分页对象
        IPage<Organization> iPage = new Page<>(currentPage, pageSize);
        // 构建查询对象
        QueryWrapper<Organization> wrapper = new QueryWrapper<>();
        if(organizationQueryVo != null){
            if(!StringUtils.isEmpty(organizationQueryVo.getTitle())){
                wrapper.like("title", organizationQueryVo.getTitle());
            }
        }
        // 调用mapper进行分页查询
        IPage<Organization> organizationIPage = organizationMapper.selectPage(iPage, wrapper);
        return new PageResult(organizationIPage.getTotal(), organizationIPage.getRecords());
    }

    /**
     * 根据机构id来修改机构状态
     * @param organizationId
     * @return
     */
    @Override
    public boolean updateStatusById(Long organizationId) {
        UpdateWrapper<Organization> updateWrapper = new UpdateWrapper<>();
        Organization organization = organizationMapper.selectById(organizationId);
        if(!StringUtils.isEmpty(organization.getStatus()) && organization.getStatus().equals("0")){
            updateWrapper.set("status", "1");
            updateWrapper.eq("id", organizationId);
            return update(updateWrapper);
        }
        if(!StringUtils.isEmpty(organization.getStatus()) && organization.getStatus().equals("1")){
            updateWrapper.set("status", "0");
            updateWrapper.eq("id", organizationId);
            return update(updateWrapper);
        }
        return false;
    }

    @Override
    public List<Organization> searchAll() {
        List<Organization> organizationList = organizationMapper.selectList(null);
        return organizationList;
    }

    /**
     * 根据机构id查询团队
     * @param organizationId
     * @return
     */
    public List<Team> queryTeamByOrganizationId(Long organizationId) {
        QueryWrapper<TeamOrganization> wrapper = new QueryWrapper<>();
        wrapper.eq("organization_id",organizationId);
        List<Team> teamList=new ArrayList<>();
        List<TeamOrganization> teamOrganizations = teamOrganizationMapper.selectList(wrapper);
        for (TeamOrganization teamOrganization : teamOrganizations) {
            Long teamId = teamOrganization.getTeamId();
            Team team = teamMapper.selectById(teamId);
            teamList.add(team);
        }
        return teamList;
    }
}
